package com.UnionPayCenter.distribution.manager.impl;

import com.UnionPayCenter.coupon.service.CouponQueryService;
import com.UnionPayCenter.distribution.entity.DistributionTemplateCardDO;
import com.UnionPayCenter.distribution.manager.DistributionTemplateCardManager;
import com.UnionPayCenter.mapper.DistributionTemplateCardDOMapper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Noah
 * @since 1.0.0, 2019/08/03
 */
@Service
public class DistributionTemplateCardManagerImpl implements DistributionTemplateCardManager {

    /**
     * 会员卡
     */
    private static final Long CARD_TYPE_MEMBERSHIP = 1L;

    /**
     * 优惠券
     */
    private static final Long CARD_TYPE_VOUCHER = 2L;

    /**
     * 分享人
     */
    private static final String CARD_OWNER_USER = "1";

    /**
     * 被分享人
     */
    private static final String CARD_OWNER_COUNTERPARTY = "2";

    @Autowired
    private DistributionTemplateCardDOMapper cardDOMapper;

    @Autowired
    private CouponQueryService couponQueryService;


    /**
     * @param cardDoList 卡券信息
     * @param shopId     店铺ID
     * @param idgen      模板ID
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveUserDistributionTemplateCard(List<DistributionTemplateCardDO> cardDoList, Long shopId, Integer idgen) {
        saveDistributionTemplateCard(cardDoList, shopId, idgen, CARD_OWNER_USER);
    }


    private void saveDistributionTemplateCard(List<DistributionTemplateCardDO> cardDoList, Long shopId, Integer idgen, String owner) {
        if (Objects.isNull(shopId) || Objects.isNull(idgen)) {
            return;
        }
        cardDOMapper.deleteByShopId(shopId, idgen, owner, null);
        if (CollectionUtils.isEmpty(cardDoList)) {
            return;
        }
        Date now = new Date();
        cardDoList = cardDoList.stream().peek(e -> {
            e.setCreateTime(now);
            e.setUpdateTime(now);
            if (Objects.isNull(e.getCardType())) {
                e.setCardType(CARD_TYPE_VOUCHER);
            }
            if (StringUtils.isBlank(e.getCardOwner())) {
                e.setCardOwner(owner);
            }
            e.setIsDelete(0);
        }).collect(Collectors.toList());
        cardDOMapper.insertBatch(cardDoList);
    }


    /**
     * @param cardDoList       卡券信息
     * @param memberShipCardId 会员卡ID
     * @param shopId           店铺ID
     * @param idgen            模板ID
     */
    @Override
    public void saveCounterpartyUserDistributionTemplateCard(List<DistributionTemplateCardDO> cardDoList, Integer memberShipCardId,
                                                             Long shopId, Integer idgen) {

        if (CollectionUtils.isEmpty(cardDoList)) {
            cardDoList = new ArrayList<>();
        }
        if (null != memberShipCardId) {
            DistributionTemplateCardDO cardDO = new DistributionTemplateCardDO();
            cardDO.setCardType(CARD_TYPE_MEMBERSHIP);
            cardDO.setCardId(String.valueOf(memberShipCardId));
            cardDO.setCardOwner(CARD_OWNER_COUNTERPARTY);
            cardDO.setShopId(shopId);
            cardDO.setIdgen(idgen);
            cardDO.setCardNumber(0);
            cardDoList.add(cardDO);
        }
        saveDistributionTemplateCard(cardDoList, shopId, idgen, CARD_OWNER_COUNTERPARTY);
    }

    /**
     * 获取模板卡券列表
     *
     * @param shopId 店铺ID
     * @param idgen  模板ID
     * @return 卡券列表
     */
    @Override
    public List<DistributionTemplateCardDO> listUserTemplateCard(Long shopId, Integer idgen) {
        List<DistributionTemplateCardDO> distributionTemplateCardDOList = cardDOMapper.listDistributionTemplateCardDO(shopId,
                idgen, CARD_OWNER_USER, CARD_TYPE_VOUCHER);
        if (CollectionUtils.isEmpty(distributionTemplateCardDOList)) {
            return Collections.emptyList();
        }
        return distributionTemplateCardDOList.stream().filter(e -> StringUtils.isNotBlank(e.getCardId())
                && couponQueryService.checkCouponValidTag(Integer.valueOf(e.getCardId()))).collect(Collectors.toList());
    }

    /**
     * 获取模板卡券列表
     *
     * @param shopId 店铺ID
     * @param idgen  模板ID
     * @return 卡券列表
     */
    @Override
    public List<DistributionTemplateCardDO> listCounterpartyUserTemplateCard(Long shopId, Integer idgen) {
        List<DistributionTemplateCardDO> cardDOList = cardDOMapper.listDistributionTemplateCardDO(shopId, idgen,
                CARD_OWNER_COUNTERPARTY, CARD_TYPE_VOUCHER);
        if (CollectionUtils.isEmpty(cardDOList)) {
            return Collections.emptyList();
        }
        return cardDOList.stream().filter(e -> StringUtils.isNotBlank(e.getCardId()) && couponQueryService.checkCouponValidTag(Integer.valueOf(e.getCardId())))
                .collect(Collectors.toList());
    }

    /**
     * 获取会员卡信息
     *
     * @param shopId 店铺ID
     * @param idgen  模板ID
     * @return 会员卡信息
     */
    @Override
    public String getMemberShipCard(Long shopId, Integer idgen) {
        List<DistributionTemplateCardDO> list =
                cardDOMapper.listDistributionTemplateCardDO(shopId, idgen, CARD_OWNER_COUNTERPARTY, CARD_TYPE_MEMBERSHIP);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0).getCardId();
    }
}
